package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.common.enums.param.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.domain.clue.ClueDO;
import com.qk.domain.clue.CluePoolDO;
import com.qk.dto.clue.*;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.entity.User;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.mapper.ClueTrackRecordMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.ClueService;
import com.qk.vo.clue.ClueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public void addClue(ClueDTO clueDTO) {
        if(ObjectUtil.isEmpty(clueDTO.getPhone())||ObjectUtil.isEmpty(clueDTO.getChannel())){
            CommonException.throwException(ParamEnum.PARAM_ERROR);
        }
        Clue clue = BeanUtil.copyProperties(clueDTO, Clue.class);
        clue.setStatus(ClueStatusConstants.STATUS_WAIT);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        this.save(clue);
    }

    @Override
    public PageResult<ClueDO> selectByPage(ClueListDTO clueListDTO) {
        PageHelper.startPage(clueListDTO.getPage(), clueListDTO.getPageSize());
        List<ClueDO> clueDOList = baseMapper.selectByPage(clueListDTO);
        Page<ClueDO> page = (Page<ClueDO>) clueDOList;
        return PageResult.<ClueDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public ClueVO getClueById(Integer id) {
        Clue clue = this.baseMapper.selectById(id);
        Integer userId = clue.getUserId();
        User  user = userMapper.selectUserById(userId);
        List<ClueTrackRecord> clueTrackRecordList = clueTrackRecordMapper.selectList(Wrappers.lambdaQuery(ClueTrackRecord.class).in(ClueTrackRecord::getClueId,id));
        ClueVO clueVO = BeanUtil.copyProperties(clue, ClueVO.class);
        List<ClueVO.ClueTrackRecordVO> list = clueTrackRecordList.stream().map(clueTrackRecord -> {
            ClueVO.ClueTrackRecordVO clueTrackRecordVO = BeanUtil.copyProperties(clueTrackRecord, ClueVO.ClueTrackRecordVO.class);
            clueTrackRecordVO.setAssignName(user.getUsername());
            return clueTrackRecordVO;
        }).toList();
        clueVO.setTrackRecords(list);
        return clueVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClueInfoById(UpdateClueInfoDTO dto) {
        Clue clue = BeanUtil.copyProperties(dto, Clue.class);
        clue.setUpdateTime(LocalDateTime.now());
        if(Objects.equals(dto.getStatus(), ClueStatusConstants.STATUS_WAIT_FOLLOW)){
            clue.setStatus(ClueStatusConstants.STATUS_FOLLOW);
        }
        this.baseMapper.updateById(clue);
        ClueTrackRecord clueTrackRecord = BeanUtil.copyProperties(dto, ClueTrackRecord.class);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecord.setId(null);
        clueTrackRecord.setClueId(clue.getId());
        clueTrackRecord.setUserId(CurrentUserContextHolders.get());
        this.clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Override
    public void toBusiness(Integer id) {
        Clue clue = this.baseMapper.selectById(id);
        clue.setStatus(ClueStatusConstants.STATUS_TO_BUSINESS);
        clue.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(clue);
        Business business = BeanUtil.copyProperties(clue, Business.class);
        business.setId(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setNextTime(null);
        business.setUserId(null);
        business.setClueId(id);
        business.setStatus(BusinessStatusConstants.STATUS_WAIT);
        this.businessMapper.insert(business);
    }

    @Override
    @Transactional
    public void toFalse(Integer id,MarkFalseClueDto dto) {
        Clue clue = this.baseMapper.selectById(id);
        clue.setStatus(ClueStatusConstants.STATUS_TO_FALSE);
        clue.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(clue);
        ClueTrackRecord clueTrackRecord = BeanUtil.copyProperties(clue, ClueTrackRecord.class);
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecord.setId(null);
        clueTrackRecord.setClueId(id);
        clueTrackRecord.setType(0);
        clueTrackRecord.setFalseReason(dto.getReason());
        clueTrackRecord.setRecord(dto.getRemark());
        clueTrackRecord.setUserId(CurrentUserContextHolders.get());
        this.clueTrackRecordMapper.insert(clueTrackRecord);
    }

    @Override
    public PageResult<CluePoolDO> selectPool(CluePoolDTO cluePoolDTO) {
        PageHelper.startPage(cluePoolDTO.getPage(), cluePoolDTO.getPageSize());
        List<CluePoolDO> cluePoolDOList = baseMapper.selectPool(cluePoolDTO);
        Page<CluePoolDO> page = (Page<CluePoolDO>) cluePoolDOList;
        return PageResult.<CluePoolDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

}